// pages/detail/index.js
import {
  getDetailById,
  likeOrCancelLikeRequest,
  deleteRequest,
} from '../../utils/dataBase';
import eventBus from '../../utils/eventBus';
Page({
  data: {
    loading: true,
    detail: {
      "id": 7,
      "title": "宣州谢朓楼饯别校书叔云",
      "content": "长风万里送秋雁，对此可以酣高楼。",
      "likeNums": 77,
      "isLiked": false,
      "show": true,
      "style": {
        "height": "auto"
      },
      "author": "李白",
      "dynasty": "唐",
      "detailContent": "弃我去者，昨日之日不可留；\n乱我心者，今日之日多烦忧。\n长风万里送秋雁，对此可以酣高楼。\n蓬莱文章建安骨，中间小谢又清发。\n俱怀逸兴壮思飞，欲上青天览明月。\n抽刀断水水更流，举杯消愁愁更愁。\n人生在世不称意，明朝散发弄扁舟。",
      "detailContentArr": [
        {
          "text": "弃我去者，昨日之日不可留；",
          "highlight": false
        },
        {
          "text": "乱我心者，今日之日多烦忧。",
          "highlight": false
        },
        {
          "text": "长风万里送秋雁，对此可以酣高楼。",
          "highlight": true
        },
        {
          "text": "蓬莱文章建安骨，中间小谢又清发。",
          "highlight": false
        },
        {
          "text": "俱怀逸兴壮思飞，欲上青天览明月。",
          "highlight": false
        },
        {
          "text": "抽刀断水水更流，举杯消愁愁更愁。",
          "highlight": false
        },
        {
          "text": "人生在世不称意，明朝散发弄扁舟。",
          "highlight": false
        }
      ]
    },
  },
  onLoad: function (options) {
    const { id } = options;
    this.getDetail(id);
  },
  async getDetail(id) {
    const detailFromGet = await getDetailById(+id);
    const detail = this.splitContentAndHighlight(detailFromGet);
    this.setData({
      loading: false,
      detail,
    });
  },
  /**
   * 将后台获取的文章详细的文章按照 "\n" 进行分割成数组，并且高亮列表进来的词句段落
   * @param {*} detailFromGet 从后台获取的数据
   */
  splitContentAndHighlight(detailFromGet) {
    const detailContentArr = detailFromGet.detailContent
      .split('\n')
      .map((item) => {
        return {
          text: item,
          highlight: item === detailFromGet.content,
        };
      });
    const detail = {
      ...detailFromGet,
      detailContentArr,
    };
    return detail;
  },
  likeOrCancelLike(e) {
    const { id } = e.currentTarget.dataset;
    const result = likeOrCancelLikeRequest(+id);
    console.log(result);
    const { isLiked, likeNums, status } = result;
    if (status === 1) {
      this.setData({
        [`detail.isLiked`]: isLiked,
        [`detail.likeNums`]: likeNums,
      });
    }
    this.noticeListPageLikeOrDislike(+id, result);
  },
  noticeListPageLikeOrDislike(id, result) {
    const deliverData = {
      id,
      result,
    };
    eventBus.emit('detailLikeOrCancelLike', deliverData);
  },
  async deleteItem(e) {
    const sureResult = await wx.showModal({
      title: '提示',
      content: '确定要删除这项吗',
    });
    if(sureResult.confirm){
      wx.showToast({
        icon: "loading",
        duration: 10000,
      });
      const { id } = e.currentTarget.dataset;
      const result = await deleteRequest(+id);
      if (result) {
        wx.hideToast();
        await wx.navigateBack();
        setTimeout(()=>{
          this.noticeListPageDelete(+id, result);
        },200)
      }
    }
  },

  noticeListPageDelete(id, result) {
    const deliverData = {
      id,
      result,
    };
    eventBus.emit('detailDelete', deliverData);
  },
});
